package com.yunxi.service.iam.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.service.sap.dubbo.Ups2SapDubboServiceImpl;
import com.yunxi.ups.pojo.UpsPostVO;
import com.yunxi.ups.pojo.UpsUserVO;
import com.yunxi.ups.service.UpsPartenrDubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/17.
 */
public class UpsPartenrDubboServiceImpl implements UpsPartenrDubboService {

    protected static final Logger logger = LoggerFactory.getLogger(Ups2SapDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${SAP.URL}")
    private String SAP_URL;
    @Value("${COMM2.URL}")
    private String COMM2_URL;
    @Value("${UPS.URL}")
    private String UPS_URL;
    @Value("${COMM.API.URL}")
    private String COMM_API_URLL;
    @Override
    public ApiResult sendIamUserVO(List<UpsUserVO> upsUserList) throws Exception {
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        Map dataMap = new HashMap();
        dataMap.put("Total", upsUserList.size());
        dataMap.put("Data", upsUserList);
        Object bodyStr = JSONObject.toJSON(dataMap);
        ApiResult apiResult = null;
        EaiLog eaiLog = new EaiLog();
        eaiLog.setPmsEaiContent(bodyStr.toString());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS系统发送人员信息数据给IAM系统");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.PMS_USER);
        try {
            Map map = HttpUtil.sendPost(bodyStr.toString(), Constant.PMS_USER, Constant.PMS, COMM2_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(COMM2_URL);
            Object object = map.get("header");
            JSONObject header = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(header.get("statusFlag"));
            if (statusFlag.equals("0")) {
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail();
            } else {
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("reslut")).equals("false")) {
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult = ResponseUtil.fail();
                } else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error("调用IAM系统，传递渠道人员信息，处理不成功！");
            eaiLog.setDisposeStatus(2);
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
            logger.debug("mq发送成功==============");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("mq发送失败==============");
            apiResult = ResponseUtil.fail();
        }
        return apiResult;
    }

    @Override
    public ApiResult sendUpsPostVO(List<UpsPostVO> upsPostVO) throws Exception {
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        Object bodyStr = JSONObject.toJSON(upsPostVO);
        ApiResult apiResult = null;
        EaiLog eaiLog = new EaiLog();
        eaiLog.setPmsEaiContent(bodyStr.toString());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS系统发送渠道岗位信息");
        eaiLog.setSourceSystem(Constant.UPSPARTS);
        eaiLog.setServiceName(Constant.UPS_POST);
        try {
            Map map = HttpUtil.sendPost(bodyStr.toString(), Constant.UPS_POST, Constant.UPSPARTS, COMM_API_URLL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(UPS_URL);
            Object object = map.get("header");
            JSONObject header = JSONObject.parseObject(JSON.toJSONString(object));
            // jsonObject.get("statusFlag");
            String statusFlag = String.valueOf(header.get("statusFlag"));
            //ESB头中的statusFlag 0失败，1成功
            if (statusFlag.equals("0")) {
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail();
            } else {
                apiResult = ResponseUtil.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("传递渠道岗位信息，处理不成功！");
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
            logger.debug("mq发送成功==============");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            logger.error("mq发送失败==============");
            apiResult = ResponseUtil.fail();
        }
        return apiResult;
    }
}
